package com.openbookformat.page;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import org.openbookformat.tags.FootnoteTag;
import org.openbookformat.tags.TagParsingException;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.BadLocationException;
import org.openide.cookies.EditorCookie;
import org.openide.util.Exceptions;

/**
 *
 * @author MUFEED
 */
public class Page {
    int pageNumber;
    EditorCookie ed;
    int offset;
    
    public Page(EditorCookie ed, int offset, int length) {
        this.ed = ed;
        this.offset = offset;
        this.length = length;
    }

    public Page(EditorCookie ed, int offset, int length, int pageNumber) {
        this(ed, offset, length);
        this.pageNumber = pageNumber;
    }
    
    public String getText() {
        try {
            return ed.getDocument().getText(offset, length);
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
        return null;
    }
    
    public boolean hasFootnote() {
        return getText().contains("<footnote>");
    }
    
    public FootnoteTag getFootnote() {
        Pattern pattern = Pattern.compile("(?s)<footnote>.*?</footnote>");
        Matcher matcher = pattern.matcher(getText());
        
        if (matcher.find()) {
            try {
                return FootnoteTag.parse(matcher.group());
            } catch (TagParsingException ex) {
                Logger.getLogger(Page.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    
    public String getBody() {
        if(hasFootnote()) {
            return getText().substring(0, 
                    getText().indexOf("<footnote>"));
        } else {
            return getText();
        }
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
    
    public void updateOffset(int diff) {
        offset += diff;
    }
    

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }
    protected int length;
    public static final String PROP_LENGTH = "length";

    /**
     * Get the value of length
     *
     * @return the value of length
     */
    public int getLength() {
        return length;
    }

    /**
     * Set the value of length
     *
     * @param length new value of length
     */
    public void setLength(int length) {
        int oldLength = this.length;
        this.length = length;
        propertyChangeSupport.firePropertyChange(PROP_LENGTH, oldLength, length);
    }
    
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    @Override
    public String toString() {
        return getText();
    }
    
    public void setText(String text) {
        try {
            ed.getDocument().remove(offset, length);
            ed.getDocument().insertString(offset, text, null);
            setLength(text.length());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    
}
